<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product_model extends CI_Model {
    function __construct()
    {
        parent::__construct();
    }

    function getcid($parent){
        $ci =& get_instance();
        $ci->db->select('CategoryID');
        $ci->db->from("Category");
        $ci->db->like("parent","$parent");
        $query=$ci->db->get();
        if($query->num_rows()>0){
            foreach($query->result_array() as $key=>$rs){
                $parent.=",".$rs['CategoryID'];
            }
        }
        $query->free_result();
        $ci->db->close();
        return $parent;
    }
    function update_product_hit($aid){
        $ci =& get_instance();
        $ci->db->where("aid","$aid");
        $ci->db->set("hit","hit+1",false);
        $ci->db->update('product');
    }

    function getallproduct($num=1,$offset=0)
    {  
	    if($offset>0){$offset=$offset*$num;}else{$offset=0;}
	    $this->db->select('aid,productname,images,tags');
	    $this->db->limit($num,$offset);
	    $this->db->from("product");
	    $this->db->order_by('orderby','asc');
	    $query = $this->db->get();
	    return $query->result_array();     
	    $query->free_result();
	    $this->db->close();
    }

    function getsearchproduct($num=1,$offset=0,$num_rows=0)
    {  
        $this->load->helper('MY_string');
        $this->load->helper('string');
        $keyword = $this->input->get('keyword');
        $keyword=substr($keyword,0,25);
        $keyword=$this->security->sanitize_filename($keyword);
        $keyword=quotes_to_entities($keyword);
        $keyword=strip_quotes($keyword);
        $keyword=quotes_to_entities($keyword);
        $keyword=urldecode($keyword);
		$keyword=str_replace("'","",$keyword);
		$keyword=str_replace(" ","",$keyword);
		$keyword= clean($keyword);     
        //echo $keyword;
	    if($offset>0){$offset=$offset*$num;}else{$offset=0;}
	    $this->db->select('aid,productname,images,tags');
	    $this->db->like("productname",$keyword);
		$this->db->or_like("tags",$keyword);
		$this->db->or_like("ProductNumber",$keyword);
		$this->db->or_like("ProductChar",$keyword);
		$this->db->or_like("Brief",$keyword); 
		$this->db->or_like("Detail",$keyword); 
	    if($num_rows!=1){
	    	$this->db->limit($num,$offset);
	     }
	    $this->db->from("product");
	    $this->db->order_by('orderby','asc');
	    $query = $this->db->get();
	    if($num_rows==1){
	    	return $query->num_rows();
	    }
	    return $query->result_array();     
	    $query->free_result();
	    $this->db->close();
    }
    function getproductcategory($categoryid,$cid=0){
        if(!is_numeric($cid)){
        	return $categoryid;
        }
    	$getcid=",".$this->getcid($categoryid).",";
    	if(strstr($getcid,",".$cid.",")){
    		$categoryid=$cid;
    	}
    	return $categoryid;
    }
    
    function getcidproduct($categoryid,$num=1,$offset=0)
    {	$categoryid=$this->getcid($categoryid);
	    if($offset>0){$offset=$offset*$num;}else{$offset=0;}
	    $this->db->select('aid,productname,images,tags');
	    $sql="categoryid in($categoryid)";
	    $this->db->where($sql);
	    $this->db->limit($num,$offset);
	    $this->db->from("product");
	    $this->db->order_by('orderby','asc');
	    $query = $this->db->get();
	    return $query->result_array();     
	    $query->free_result();
	    $this->db->close();
    }
       
    function category($parent){
	    $this->db->order_by('orderby','asc');
	    $this->db->like("Parent",$parent);
	    $query = $this->db->get("category");	     
	    return $query->result_array();
    }
    
    
    function categorycount($categoryid=0){
	    $categoryid=$this->getcid($categoryid);
	    $sql="categoryid in($categoryid)";
	    $this->db->where($sql);
	    $query=$this->db->get("product");       
	    return $query->num_rows();
    }  
    function getcategoryname($categoryid)
    {
	    $this->db->select('CategoryName');
	    $this->db->where("categoryid","$categoryid");
	    $query = $this->db->get('category');
	    if ($query->num_rows()>0) {
		    foreach($query->result_array() as $key=>$rs)
		    {
		    	$row=$rs['CategoryName'];
		    }
		    $query->free_result();
		    $this->db->close();
		    return $row;
	    }

    }
    
    function getoneproduct($aid)
    {   
    	if(!is_numeric($aid)){show_404();}
        $this->db->limit(1);
	    $this->db->where('aid',"$aid");
	    $query = $this->db->get('product');	   
	    if ($query->num_rows()>0) {
	    	$this->update_product_hit($aid);
			$list=$query->result_array();
		    $query->free_result();
		    $this->db->close();
		    foreach($list as $key=>$rs){
		 		$rs[$key]=$rs;
		 	}
		    return $rs;
	    }else{
	    	show_404();
	    }

    }

}
